package com.personnalManagerSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.personnalManagerSystem.model.User;
import com.personnalManagerSystem.service.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;


import java.util.List;

/**
 * @Author liqing
 * @Date: 2022/3/27 13:12
 * @Description
 */
@Controller //封装的一个工具类,在一个页面中用到多个服务层对象,重复创建过于繁琐,且这里封装了分页
public class BaseController {
    @Autowired
    IUserService iUserService;
    @Autowired
    IRoleService iRoleService;
    @Autowired
    IUserRoleService iUserRoleService;
    @Autowired
    IPermissionService iPermissionService;
    @Autowired
    IRolePermissionService iRolePermissionService;
    @Autowired
    IDepartmentService iDepartmentService;
    @Autowired
    IUserDepartmentService iUserDepartmentService;
    @Autowired
    ILeaveeService iLeaveeService;
    @Autowired
    IAttendService iAttendService;
    @Autowired
    IWageService iWageService;
    @Autowired
    IPositionService iPositionService;
    @Autowired
    IDepartmentPositionService iDepartmentPositionService;
    @Autowired
    INoticeService iNoticeService;
    @Autowired
    IRecruitService iRecruitService;
    @Autowired
    IInterviewService iInterviewService;
    @Autowired
    ICompanyIncomeService iCompanyIncomeService;

    public <T> Model baseList(Model model,
                              String option,
                              String searchContent,
                              Integer pageNum,
                              Integer pageSize,
                              IService<T> iService){
        //设置默认的页面
        if(pageNum==null  || pageNum==0){
            pageNum=1;
        }
        //设置默认的一页显示的记录数
        if(pageSize==null  || pageSize==0){
            pageSize= 5;
        }
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        if(option!=null
                && searchContent!=null
                && !"".equals(option)
                &&!"".equals(searchContent)){
            //就进行模糊查询
            wrapper.like(option,searchContent);

        }
        /**
         * @Description 在SSM阶段中分页也是利用pageHelper插件,其步骤如下:
         * 1.开启分页
         * pageHelper.startPage(pageNum,pageSize);
         * 2.查询所有的数据
         * mapper.selectAll();
         * 3.将查询的数据封装在pageInfo<T>对象中
         * PageInfo<T>  info = new PageInfo<>(mapper.selectAll())
         * 4.将结果封装在map对象中,然后返回数据
         * 下面这个分页其原理大致相同
         *
         */
        //设置分页
        Page<List<T>> pageInfo = PageHelper.startPage(pageNum, pageSize);//返回值是Page类型的数据
        //在登录的时候将用户记录存储在了session中
        User user =(User) SecurityUtils.getSubject().getSession().getAttribute("user");
        if("普通用户".equals(user.getDescription())){
            //如果其角色是普通用户,那么只可以查询关于自己的一系列消息,不能出现跨用户访问,否则会出现资源泄露
            wrapper.eq("user_id",user.getUserId());
        }
        //查询数据
        iService.list(wrapper);
        System.out.println("pageInfo:"+pageInfo);
        //设置页面信息
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("option",option);
        model.addAttribute("searchContent",searchContent);
        return model;
    }




}
